package com.learn.controller;

import com.learn.pojo.Books;
import com.learn.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Copyright: Copyright (c) 2020 Asiainfo
 *
 * @ClassName: com.learn.controller.BookController
 * @Description: 该类的功能描述
 * @version: v1.0.0
 * @author: lipan
 * @date: 2020/2/10 13:50
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2020/2/10      lipan          v1.0.0               修改原因
 */

@Controller
@RequestMapping("/book")
public class BookController {




    @Autowired
    private BooksService booksService;

    @RequestMapping("/queryAll")
    public String getAllBooks(Model model){
        List<Books> books = booksService.findAll();
        model.addAttribute("bookList",books);
         return "AllBook";
    }


    @RequestMapping("/toAddBook")
    public String toAddBookPage(){
          return  "addBook";
    }


    @RequestMapping("/addBook")
    public String addBook(Books book , HttpServletRequest request){
        //表单提交的name与实体类属性一致springmvc会封装到对象中，不一致需要从request中去取
        System.out.println(request.getParameter("detail"));
        booksService.addBook(book);
        return "redirect:/book/queryAll";
    }

    //restful风格参数获取
    @RequestMapping("/del/{bookId}")
    public String delBook(@PathVariable int bookId){
        booksService.deleteBook(bookId);

        return "redirect:/book/queryAll";
    }


    //用问号传参的字段名和参数名一致可以直接接收
    @RequestMapping("/toUpdateBook")
    public  String toUpdateBook(Model model,int id){
        Books book = booksService.getBookById(id);
        model.addAttribute("book",book);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public  String updateBook(Books book){
        booksService.updateBook(book);
        return "redirect:/book/queryAll";
    }

}
